/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


/*************************************************************************
 *\author: starlove
 *************************************************************************/
#ifndef __CC_PYTHON_IMPORT_O_IMAGE_IMPL__
#define __CC_PYTHON_IMPORT_O_IMAGE_IMPL__

#include "python_object_image.h"

NS_CCPY_BEGIN
	
	PYCLASS_INIT_AND_BASE(CPyImage, CPyCCObject, "CCImage", "Python CCImage Object");
	PYCLASS_BEGIN_METHOD_DEF(CPyImage)
	PYCLASS_ADD_METHOD_EX(CPyImage, initWithImageFile, METH_KEYWORDS|METH_VARARGS)
	PYCLASS_ADD_METHOD_EX(CPyImage, initWithImageFileThreadSafe, METH_KEYWORDS|METH_VARARGS)
	PYCLASS_ADD_METHOD_EX(CPyImage, initWithImageData, METH_KEYWORDS|METH_VARARGS)
	PYCLASS_ADD_METHOD_EX(CPyImage, initWithString, METH_KEYWORDS|METH_VARARGS)
	PYCLASS_ADD_METHOD_EX(CPyImage, saveToFile, METH_KEYWORDS|METH_VARARGS)
	PYCLASS_END_METHOD_DEF(CPyImage)
	PYCLASS_BEGIN_GETSET_DEF(CPyImage)
	PYCLASS_ADD_GET(CPyImage, _Data)
	PYCLASS_ADD_GET(CPyImage, _hasAlpha)
	PYCLASS_ADD_GET(CPyImage, _isPremultipliedAlpha)
	PYCLASS_ADD_GET(CPyImage, _Width)
	PYCLASS_ADD_GET(CPyImage, _Height)
	PYCLASS_ADD_GET(CPyImage, _BitsPerComponent)
	PYCLASS_END_GETSET_DEF(CPyImage)
	PYCLASS_IMP_INTERFACE_NEW(CPyImage)
			
	int CPyImage::__init__(CPyImage *pySelf, PyObject *args, PyObject *kwds)
	{
		pySelf->setRaw();
		return 0;
	}

	void CPyImage::__del__(CPyImage *self)
	{
		self->ob_type->tp_free(self);
	}
	
	PYCLASS_METHOD_KW_IMPL(CPyImage, initWithImageFile)
	{
		if( !pyArgs )
			Py_RETURN_FALSE;
		cocos2d::CCNode *node = NULL;
		static char* kwlist[] = {"path", "imgtype", NULL};
		char *path	= "";
		int imgtype	= cocos2d::CCImage::kFmtPng;
		if(!PyArg_ParseTupleAndKeywords(pyArgs,pyKW,"s|I",kwlist,&path,&imgtype))
			Py_RETURN_FALSE;
		if(!path || strlen(path) == 0)
			Py_RETURN_FALSE;

		CC_METHOD_WRAPPER_GETER(cocos2d::CCImage *, CPyImage, initWithImageFile, bool, path, static_cast<cocos2d::CCImage::EImageFormat>(imgtype));
		Py_RETURN_BOOL(v);
	}

	PYCLASS_METHOD_KW_IMPL(CPyImage, initWithImageFileThreadSafe)
	{
		if( !pyArgs )
			Py_RETURN_FALSE;
		cocos2d::CCNode *node = NULL;
		static char* kwlist[] = {"path", "imgtype", NULL};
		char *path	= "";
		int imgtype	= cocos2d::CCImage::kFmtPng;
		if(!PyArg_ParseTupleAndKeywords(pyArgs,pyKW,"s|I",kwlist,&path,&imgtype))
			Py_RETURN_FALSE;
		if(!path || strlen(path) == 0)
			Py_RETURN_FALSE;

		CC_METHOD_WRAPPER_GETER(cocos2d::CCImage *, CPyImage, initWithImageFileThreadSafe, bool, path, static_cast<cocos2d::CCImage::EImageFormat>(imgtype));
		Py_RETURN_BOOL(v);
	}

	PYCLASS_METHOD_KW_IMPL(CPyImage, initWithImageData)
	{
		if( !pyArgs )
			Py_RETURN_FALSE;
		cocos2d::CCNode *node = NULL;
		static char* kwlist[] = {"data", "imgtype", "w", "h", "bitsPerComponent", NULL};
		PyObject *pyData = NULL;
		int imgtype	= cocos2d::CCImage::kFmtPng;
		int w = 0;
		int h = 0;
		int bpc = 0;
		if(!PyArg_ParseTupleAndKeywords(pyArgs,pyKW,"s|Iiii",kwlist, &pyData, &imgtype, &w, &h, &bpc))
			Py_RETURN_FALSE;
		if(pyData == Py_None || !PyObject_TypeCheck(pyData, CPyClassBase::GetTypeObject<CPyPointer>()))
			Py_RETURN_FALSE;

		CPyPointer *pyPtr = (CPyPointer *)pyData;
		if(!pyPtr->getPtr())
			Py_RETURN_FALSE;

		CC_METHOD_WRAPPER_GETER(cocos2d::CCImage *, CPyImage, initWithImageData, bool, pyPtr->getPtr(), pyPtr->getSize(), static_cast<cocos2d::CCImage::EImageFormat>(imgtype), w, h, bpc);
		Py_RETURN_BOOL(v);
	}
	
	PYCLASS_METHOD_KW_IMPL(CPyImage, initWithString)
	{
		if( !pyArgs )
			Py_RETURN_FALSE;
		static char* kwlist[] = {"text", "w", "h", "align", "fontName", "fontSize", NULL};
		char *text = "";
		int align	= cocos2d::CCImage::kAlignCenter;
		int w = 0;
		int h = 0;
		char *fontname = "";
		int s = 0;
		if(!PyArg_ParseTupleAndKeywords(pyArgs,pyKW,"s|iiIsI",kwlist, &text, &w, &h, &align, &fontname, &s))
			Py_RETURN_FALSE;
		if(!text || strlen(text) == 0)
			Py_RETURN_FALSE;
		
		CC_METHOD_WRAPPER_GETER(cocos2d::CCImage *, CPyImage, initWithString, bool, text, w, h, static_cast<cocos2d::CCImage::ETextAlign>(align), fontname, s);
		Py_RETURN_BOOL(v);
	}
	
	PYCLASS_METHOD_KW_IMPL(CPyImage, saveToFile)
	{
		if( !pyArgs )
			Py_RETURN_FALSE;
		static char* kwlist[] = {"path", "isRGB", NULL};
		char *path = "";
		PyObject *pisRGB = Py_False;
		if(!PyArg_ParseTupleAndKeywords(pyArgs,pyKW,"s|O",kwlist, &path, &pisRGB))
			Py_RETURN_FALSE;
		if(!path || strlen(path) == 0)
			Py_RETURN_FALSE;
		bool isRGB = pisRGB != Py_False;
		
		CC_METHOD_WRAPPER_GETER(cocos2d::CCImage *, CPyImage, saveToFile, bool, path, isRGB);
		Py_RETURN_BOOL(v);
	}
		
	PYCLASS_GET_IMPL(CPyImage, _Data)
	{
		void *d = NULL;
		unsigned int s = 0;
		do
		{
			CC_METHOD_WRAPPER_GETER(cocos2d::CCImage *, CPyImage, getData, void *);
			d = v;
		}while(0);
		do
		{
			CC_METHOD_WRAPPER_GETER(cocos2d::CCImage *, CPyImage, getDataLen, unsigned int);
			s = v;
		}while(0);
		CPyPointer *ptr = CPyPointer::create(d, s);
		if(ptr)
			return (PyObject *)ptr;
		Py_RETURN_NONE;
	}
		
	PYCLASS_GET_IMPL(CPyImage, _hasAlpha)
	{
		CC_METHOD_WRAPPER_GETER(cocos2d::CCImage *, CPyImage, hasAlpha, bool);
		Py_RETURN_BOOL(v);
	}
		
	PYCLASS_GET_IMPL(CPyImage, _isPremultipliedAlpha)
	{
		CC_METHOD_WRAPPER_GETER(cocos2d::CCImage *, CPyImage, isPremultipliedAlpha, bool);
		Py_RETURN_BOOL(v);
	}
	
	PYCLASS_GET_IMPL(CPyImage, _Width)
	{
		CC_METHOD_WRAPPER_GETER(cocos2d::CCImage *, CPyImage, getWidth, unsigned short);
		return Py_BuildValue("I", v);
	}
	
	PYCLASS_GET_IMPL(CPyImage, _Height)
	{
		CC_METHOD_WRAPPER_GETER(cocos2d::CCImage *, CPyImage, getHeight, unsigned short);
		return Py_BuildValue("I", v);
	}
	
	PYCLASS_GET_IMPL(CPyImage, _BitsPerComponent)
	{
		CC_METHOD_WRAPPER_GETER(cocos2d::CCImage *, CPyImage, getBitsPerComponent, int);
		return Py_BuildValue("i", v);
	}

NS_CCPY_END

#endif